package com.efast.cafe.portal.controller.statemach;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.cafe.commponent.statemach.service.IPortalStateMachTaskService;
import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.controller.AbstractController;
import com.efast.cafe.framework.enums.ResultStatus;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.framework.vo.DataTableVo;
import com.efast.cafe.portal.bean.statemach.PortalStateMachConf;
import com.efast.cafe.portal.bean.statemach.PortalStateMachConfQueryBean;
import com.efast.cafe.portal.service.statemach.IPortalStateMachConfService;

@Controller
@RequestMapping("/statemach")
public class PortalStateMachConfController extends AbstractController {

	private IPortalStateMachConfService portalStateMachConfService;
	

	@Autowired
	public void setPortalStateMachConfService(IPortalStateMachConfService portalStateMachConfService) {
		this.portalStateMachConfService = portalStateMachConfService;
	}

	/**
	 * 企业状态机配置编辑
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/conf/edit")
	public @ResponseBody ResultBean confEdit(HttpServletRequest request, HttpServletResponse response,
			@RequestBody PortalStateMachConf conf) {
		ResultBean result = new ResultBean(true, null);
		try {
			conf.setLastUpdateUser(SpringWebUtils.getRequestUser());
			portalStateMachConfService.updateConf(conf);
		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}

		return result;

	}

	/**
	 * 企业状态机配置新增
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/conf/add")
	public @ResponseBody ResultBean confAdd(HttpServletRequest request, HttpServletResponse response,
			@RequestBody PortalStateMachConf conf) {
		ResultBean result = new ResultBean(true, null);
		try {
			conf.setCompanyCode(SpringWebUtils.getRequestCompany());
			conf.setCreateUser(SpringWebUtils.getRequestUser());
			portalStateMachConfService.insertConf(conf);
		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}

		return result;
	}

	/**
	 * 企业状态机配置删除
	 * 
	 * @param roleId
	 * @return
	 */
	@RequestMapping(value = "/conf/del")
	public @ResponseBody ResultBean confDel(@RequestBody PortalStateMachConf conf) {
		ResultBean result = new ResultBean(true, null);
		try {
			conf.setLastUpdateUser(SpringWebUtils.getRequestUser());
			portalStateMachConfService.deleteConf(conf);
		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}

		return result;
	}

	/**
	 * 获取企业状态机配置列表
	 * 
	 * @param QueryBean
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/conf/list")
	public @ResponseBody DataTableVo<PortalStateMachConfQueryBean> queryAllObjectList(
			@RequestBody PortalStateMachConfQueryBean QueryBean, HttpServletRequest request) {
		QueryBean.setCompanyCode(SpringWebUtils.getRequestCompany());
		return outPageJSONArray_bootstrapDatatable(portalStateMachConfService.queryPageStateMachConf(QueryBean));
	}

	/**
	 * 获取可配置的状态
	 * 
	 * @param QueryBean
	 * @return
	 */
	@RequestMapping(value = "/conf/canconflist")
	public @ResponseBody ResultBean getCanConfigState(@RequestBody PortalStateMachConfQueryBean QueryBean) {
		ResultBean result = new ResultBean(true, null);
		try {
			result.setObject(portalStateMachConfService.getConfListByCompanyCode(SpringWebUtils.getRequestCompany()));
		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}
		return result;
	}

	/**
	 * 根据Mxgraph图处理状态机配置
	 * 
	 * @param conf
	 * @return
	 */
	@RequestMapping(value = "/conf/submitconfig")
	public @ResponseBody ResultBean submitConfigState(@RequestBody PortalStateMachConf conf) {
		ResultBean result = new ResultBean(true, null);
		try {
			if (conf.getId() == null || conf.getId() == 0) {
				portalStateMachConfService.insertConfByMxgraph(conf);
			} else {
				portalStateMachConfService.updateConfByMxgraph(conf);
			}
		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}
		return result;
	}

	@Autowired
	private IPortalStateMachTaskService taskService;
	
	@RequestMapping(value = "/conf/confinfo")
	public @ResponseBody ResultBean getConfById(@RequestBody PortalStateMachConf conf) {
		ResultBean result = new ResultBean(true, null);
		try {
			result.setObject(portalStateMachConfService.getConfById(conf.getId()));
			//taskService.test("");
		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}
		return result;
	}
	
	@RequestMapping(value = "/conf/confinfobytask")
	public @ResponseBody ResultBean getConfByTaskId(@RequestBody PortalStateMachConf conf) {
		ResultBean result = new ResultBean(true, null);
		try {
			result.setObject(portalStateMachConfService.getConfBytaskId(conf.getId()));
		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}
		return result;
	}
}
